package com.xh.personsystem.controller;

import com.github.pagehelper.PageHelper;
import com.xh.personsystem.entity.PersonInfo;
import com.xh.personsystem.entity.Result;
import com.xh.personsystem.service.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author Admins
 */
@RestController
@RequestMapping("/api")
public class PersonController {

    @Autowired
    private PersonServiceImpl personServiceImpl;

    @PostMapping("/person_info/{pageNum}/{pageSize}")
    public synchronized Object persist(@PathVariable(value = "pageNum") int pageNum, @PathVariable(value = "pageSize") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<PersonInfo> personInfos = personServiceImpl.findAll();

        assert personInfos != null;

        //为为空为true 不为空为false
        if (!CollectionUtils.isEmpty(personInfos)) {
            return new Result().success(Collections.singletonList(personInfos));
        }
        return new Result().fullError(-1, "发生错误");
    }

    @PostMapping("/person_count")
    public synchronized Object personCount() {
        return personServiceImpl.findCount();
    }

    @PostMapping("/person_delbyid/{id}")
    public synchronized Object personCount(@PathVariable("id") Integer id) {
        System.out.println("id = " + id);
        Integer deleteById = personServiceImpl.deleteById(id);
        System.out.println("deleteById = " + deleteById);

        if (deleteById > 0){
            return 1;
        }

        return 0;
    }

}
